package com.gexiaobao.pigeon.ui.fragment.setting

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.util.CacheUtil
import com.gexiaobao.pigeon.databinding.FragmentGesturePwdBinding
import com.gexiaobao.pigeon.ui.fragment.setting.pattern.PatternCheckingActivity
import com.gexiaobao.pigeon.ui.fragment.setting.pattern.PatternSettingActivity
import com.gexiaobao.pigeon.viewmodel.SettingViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 18:37
 * @Describe    : 手势密码
 */
class FragmentGesturePwd : BaseFragment<SettingViewModel, FragmentGesturePwdBinding>() {

    private var isSwitchOn = false

    override fun initView(savedInstanceState: Bundle?) {

        mDatabind.includeBar.toolbar.initClose("手势密码") {
            nav().navigateUp()
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.mineSettingShoushiOpenSw, mDatabind.mineShoushiEdit) {
            when (it) {
                mDatabind.mineSettingShoushiOpenSw -> {
                    if (isSwitchOn) {
                        isSwitchOn = false
                        mDatabind.mineShoushiEdit.visibility = View.GONE
                        CacheUtil.setPatternTurn(0)
                        CacheUtil.setPattern(0)
                        mDatabind.mineSettingShoushiOpenSw.isChecked = isSwitchOn
                    } else {
                        isSwitchOn = true
//                        val hasPattern = CacheUtil.getPattern()
                        val intent = Intent(activity, PatternSettingActivity::class.java)
                        startActivityForResult(intent, 0)
                    }
                }

                mDatabind.mineShoushiEdit -> {
                    if (CacheUtil.getPattern() == 0) {
                        startActivity<PatternSettingActivity>()
                    } else {
                        val intent = Intent(activity, PatternCheckingActivity::class.java)
                        startActivityForResult(intent, 0)
                    }
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        val pattern = CacheUtil.getPatternTurn()
        isSwitchOn = when (pattern) {
            1 -> true
            else -> false
        }
        displayChangePattern(isSwitchOn)
    }

    private fun displayChangePattern(isShowed: Boolean) {
        if (isShowed) {
            mDatabind.mineShoushiEdit.visibility = View.VISIBLE
            CacheUtil.setPatternTurn(1)
        } else {
            mDatabind.mineShoushiEdit.visibility = View.GONE
            CacheUtil.setPatternTurn(0)
        }
        mDatabind.mineSettingShoushiOpenSw.isChecked = isShowed
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 0 && resultCode == 1) {
            val result = data!!.getStringExtra("result")
            if ("true" == result) {
                displayChangePattern(true)
                CacheUtil.setPattern(1)
            } else {
                displayChangePattern(false)
                CacheUtil.setPattern(0)
            }
        }

        if (requestCode == 0 && resultCode == Activity.RESULT_OK) {
            val result = data!!.getStringExtra("result")
            if ("true" == result) {
                val intent = Intent(activity, PatternSettingActivity::class.java)
                startActivityForResult(intent, 0)
            } else {
            }
        }
    }
}